package com.aster.common.api.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aster.common.api.domain.TbEscortUserInfo;
import com.aster.common.api.service.EscortUserInfoService;
import com.aster.common.api.service.criteria.EscortUserInfoCriteria;
import com.aster.common.base.BaseController;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * <br>
 * <b>功能：</b>TbEscortUserInfoController<br>
 *   <br>
 */
@RestController
@RequestMapping("/escortUserInfo")
public class EscortUserInfoController extends BaseController {
	
	private final static Logger log= Logger.getLogger(EscortUserInfoController.class);
	@Autowired(required=false) 
	private EscortUserInfoService escortUserInfoService;

	@RequestMapping("/list")
	R list(@RequestParam Map<String, Object> params){
		try {
			Query query = new Query(params);
			EscortUserInfoCriteria criteria = new EscortUserInfoCriteria();
			if(query.getLimit() != 0 && query.getPage() != 0){
				criteria.setPageSize(query.getLimit());
				criteria.setCurrentPage(query.getPage());
			}
			if(query.containsKey("name")){
				Map<String, Object> map = new HashMap<>();
				map.put("name",query.get("name"));
				criteria.setConditionLike(map);
			}
			if(query.containsKey("sex")){
				criteria.setSex((Integer) query.get("sex"));
			}
			if(query.containsKey("timeStart")){
				Date date = DateUtil.YYYYMMDD2Date((String) query.get("timeStart"));
				criteria.setTimeStart(date);
			}
			if(query.containsKey("timeEnd")){
				Date date = DateUtil.YYYYMMDD2Date((String) query.get("timeEnd"));
				criteria.setTimeEnd(date);
			}
			if(query.containsKey("ageStart")){
				criteria.setAgeStart((Integer) query.get("ageStart"));
			}
			if(query.containsKey("ageEnd")){
				criteria.setAgeEnd((Integer) query.get("ageEnd"));
			}
			return R.ok().put("page",escortUserInfoService.getEscortUserInfoList(criteria));
		}catch (ServiceException e){
			e.printStackTrace();
			return R.error(e.getMessage());
		}catch (Exception e){
			e.printStackTrace();
			return R.error(e.getMessage());
		}
	}

	@RequestMapping("/getByIdAndDuty")
	R getByIdAndDuty(Integer id,String timeStart,String timeEnd){
		try{
			if(id == null || id == 0){
				return R.error("参数异常");
			}
			EscortUserInfoCriteria criteria = new EscortUserInfoCriteria();
			if(StringUtils.isNotEmpty(timeStart)){
				Date date = DateUtil.YYYYMMDD2Date(timeStart);
				criteria.setTimeStart(date);
			}
			else{
				Date date = DateUtil.nowZeroDateTime();
				criteria.setTimeStart(date);
			}
			if(StringUtils.isNotEmpty(timeEnd)){
				Date date = DateUtil.YYYYMMDD2Date(timeEnd);
				criteria.setTimeEnd(date);
			}
			else{
				Date date = DateUtil.addDate(criteria.getTimeStart(),30);
				criteria.setTimeEnd(date);
			}
			criteria.put("id",id);
			PageUtils pageUtils = escortUserInfoService.getEscortUserInfoList(criteria);
			if(pageUtils == null){
				return R.ok().put("data",null);
			}
			if(pageUtils.getRows() == null || pageUtils.getRows().size() == 0){
				return R.ok().put("data",null);
			}
			return R.ok().put("data",pageUtils.getRows().get(0));
		}catch (Exception ex){
			return R.error(ex.getMessage());
		}
	}

}
